package com.minshenglife.zanzan.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.RedFlowerDao;
import com.minshenglife.zanzan.dao.ReviewDao;
import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.entity.RedFlower;
import com.minshenglife.zanzan.entity.Review;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.result.ReviewResult;
import com.minshenglife.zanzan.service.ReviewService;


/**
 * 评论业务层service
 * @author huodd
 *
 */
@Component
@Transactional
public class ReviewServiceImpl implements ReviewService {
	Logger log = LoggerFactory.getLogger(ReviewServiceImpl.class);
	
	@Resource
	ReviewDao reviewDao;
	@Resource
	private UserDao userDao;
	@Resource
	RedFlowerDao flowerDao;
	
	@Override
	public void add(Review review) {
		log.debug("start add method");
		//添加新鲜事
		reviewDao.add(review);
		log.debug("end add method");
	}
	
	@Override
	public void addReviewInfo(Review review) {
		log.debug("start addReviewInfo method");
		//添加评论
		reviewDao.add(review);
		int num=1;//红花获得数量
		Double hb=0.2;//1片花瓣
		
		//评论的同时就是送红花，更新被评论用户红花数+1、
		User user=userDao.get(new Long(review.getByCriticsId()));
		user.setRedFlowerNum(user.getRedFlowerNum()+num);
		userDao.update(user);
		//添加 得花人的 红花流水
		flowerDao.add(new RedFlower(review.getCriticsId(),review.getByCriticsId(),num,Keys.REDFLOWER_STATE_ZERO));
				
		//送一朵红花者得一片花瓣，五片花瓣为一朵红花(评论的人就是送花的人)
		User user2=userDao.getUserByUserId(review.getCriticsId());
		user2.setFdFlowerNum(user2.getFdFlowerNum()+hb);
		user2.setSendFlowerNum(user.getSendFlowerNum()+num);
		user2.setSendFlowerTime(new Date());
		userDao.update(user2);
		//添加 送花人的 系统回馈红花流水
		flowerDao.add(new RedFlower(Keys.SYSTEM_SUPER_USER,review.getCriticsId(),hb,Keys.REDFLOWER_STATE_ZERO));
				
		log.debug("end addReviewInfo method");
	}
	
	@Override
	public void addReviewTxtInfo(Review review) {
		log.debug("start addReviewTxtInfo method");
		review.setReviewType(Keys.REVIEW_TYPE_ONE);
		//添加评论
		reviewDao.add(review);
		int num=1;//红花获得数量
		Double hb=0.2;//1片花瓣
		
		//评论的同时就是送红花，更新被评论用户红花数+1、
		User user=userDao.getUserByUserId(review.getByCriticsId());
		user.setRedFlowerNum(user.getRedFlowerNum()+num);
		userDao.update(user);
		//添加 得花人的 红花流水
		flowerDao.add(new RedFlower(review.getCriticsId(),review.getByCriticsId(),num,Keys.REDFLOWER_STATE_ZERO));
				
		//送一朵红花者得一片花瓣，五片花瓣为一朵红花(评论的人就是送花的人);更新送花数量与送花时间
		User user2=userDao.getUserByUserId(review.getCriticsId());
		user2.setFdFlowerNum(user2.getFdFlowerNum()+hb);
		user2.setSendFlowerNum(user2.getSendFlowerNum()+num);
		user2.setSendFlowerTime(new Date());
		userDao.update(user2);
		//添加 送花人的 系统回馈红花流水
		flowerDao.add(new RedFlower(Keys.SYSTEM_SUPER_USER,review.getCriticsId(),hb,Keys.REDFLOWER_STATE_ZERO));
						
		log.debug("end addReviewTxtInfo method");
		
	}
	

	@Override
	@Transactional(readOnly=true)
	public Page<ReviewResult> getReviewMeInfo(String empId, int pageNo, int pageSize) {
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("byCriticsId", empId);
		map.put("isLimitType", "true");
		return findReviewInfo(map,pageNo,pageSize);
	}

	@Override
	@Transactional(readOnly=true)
	public Page<ReviewResult> getMyReviewInfo(String empId, int pageNo, int pageSize) {
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("criticsId", empId);
		map.put("isLimitType", "true");
		return findReviewInfo(map,pageNo,pageSize);
	}


	@Override
	@Transactional(readOnly=true)
	public Page<ReviewResult> findReviewInfo(Map<String,Object> map, int pageNo, int pageSize) {
		if(map==null)
			map=new HashMap<String,Object>();
		int offset = (pageNo-1) * pageSize;
        Long count =reviewDao.countByParams(map);
        map.put("from", offset);
        map.put("to", offset+pageSize);
        List<ReviewResult> rows =reviewDao.findByParams(map);
        Page<ReviewResult> ret = new Page<ReviewResult>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
        return ret;
	}

}
